# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime

from odoo.exceptions import UserError

import logging
_logger = logging.getLogger(__name__)

class CrEmpDep(models.Model):

    _name = "cr.emp.dep"
    _description = "部门"

    name = fields.Char(string='部门名称')


    # @api.model
    # def create(self, values):
    #     if 'name' in values.keys():
    #         _logger.info('111111111111111111111111111')
    #         result=self.env['cr.emp.dep'].search([('name','=',values['name'])])
    #         if len(result)>1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpDep, self).create(values)



    # @api.model
    # def write(self,values):
    #     if 'name' in values.keys():
    #         result=self.env['cr.emp.dep'].search([('name','=',values['name'])])
    #         if len(result)>1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpDep, self).write(values)


class CrEmpWs(models.Model):

    _name = "cr.emp.workshop"
    _description = "车间"

    name = fields.Char(string='车间名称')
    dep_id = fields.Many2one('cr.emp.dep',string='所属部门')
    #
    # @api.model
    # def create(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.workshop'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpWs, self).create(values)

    #
    # @api.model
    # def write(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.workshop'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpWs, self).write(values)


class CrEmpTeam(models.Model):

    _name = "cr.emp.team"
    _description = "班组"

    name = fields.Char(string='班组名称')
    dep_id = fields.Many2one('cr.emp.dep',string='所属部门')
    ws_id = fields.Many2one('cr.emp.workshop',string='所属车间')
    #
    # @api.model
    # def create(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.team'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpTeam, self).create(values)


    #
    # @api.model
    # def write(self,values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.team'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpTeam, self).write(values)


class CrEmpPost(models.Model):

    _name = "cr.emp.post"
    _description = "岗位"

    name = fields.Char(string='岗位名称')
    #
    # @api.model
    # def create(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.post'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpPost, self).create(values)


    # @api.model
    # def write(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.post'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpPost, self).write(values)


class CrEmpFaultType(models.Model):

    _name = "cr.emp.fault.type"
    _description = "问题分类"

    name = fields.Char(string='问题分类名称')
    is_important = fields.Boolean(string='是否为重要问题')

    # @api.model
    # def create(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.fault.type'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpFaultType, self).create(values)
    # @api.model
    # def write(self, values):
    #     if 'name' in values.keys():
    #         result = self.env['cr.emp.fault.type'].search([('name', '=', values['name'])])
    #         if len(result) > 1:
    #             raise UserError('名称已存在')
    #     else:
    #         return super(CrEmpFaultType, self).write(values)


class CrEmpFaultNature(models.Model):

    _name = "cr.emp.fault.nature"
    _description = "问题性质"

    def get_test_data(self):
        test_data={'type' : 'self','name' : '问题性质名称','is_default' : True,'tolerate_correct': True,'sub_score' : -2,'to_lose ': True,'add_score' : 2,'priority' : 1,'fight' : '互斥关系'}
    type = fields.Selection([
        ('self', '自我暴露'),
        ('other', '他人指出'),
        ('check', '检查发现')], string= '问题性质类别')
    name = fields.Char(string='问题性质名称')
    is_default = fields.Boolean(string='是否默认')
    tolerate_correct = fields.Boolean(string='容/糾錯')
    sub_score = fields.Integer(string='扣分')
    to_lose = fields.Boolean(string='直接失信')
    add_score = fields.Integer(string='加分')
    priority = fields.Integer(string='优先级')
    fight = fields.Char(string='互斥关系')



class CrEmpUser(models.Model):

    # _name = "cr.emp.fault.nature"
    _inherit = 'res.users'
    _description = "员工端用户"
    card_id = fields.Char('工号')
    cr_emp_type = fields.Selection([('none','无权限'),
                                    ('admin', '员工端管理员'),
                                    ('station','车站领导'),
                                    ('workshop','车间领导')],default='none')
    cr_emp_ws_id = fields.Many2one('cr.emp.workshop',string='所属车间')